package com.huika.dealontop.control.nearby.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.ZoomButtonsController;

import com.huika.dealontop.R;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.BaseAct;

import com.huika.dealontop.support.domain.ProductDetailsBean;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.rsa.RsaHelper;
import com.huika.dealontop.utils.DialogUtils;
import com.huika.dealontop.utils.UIUtils;
import com.huika.dealontop.views.CommonTitleRightWindow;
import com.huika.dealontop.views.IOSAlertDialog;

import com.huika.dealontop.support.domain.MerchantDetailBean;

import com.huika.dealontop.views.TitleBarHelper;
import com.huika.dealontop.views.webview.CustomWebView;
import com.huika.huikalib.utils.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by huhao on 2015/11/25.
 */
public class MerchantIntroductionActivity extends BaseAct {
    private TextView tv_produce_name, tv_produce_time, tv_produce_tel, tv_produce_location;
    private TextView tv_wifi, tv_drinks, tv_parking, tv_gift, tv_dessert, tv_door;
    public static final String Introduce_NAME = "merchantName";
    public static final String Introduce_TIME = "saleTime";
    public static final String Merchant_PHONE = "merchantPhone";
    public static final String Merchant_EXT = "merchantExt";
    public static final String Merchant_ADDRESS = "merchantAddress";
    public static final String Merchant_SERVICE = "merchantService";
    public static final String Merchant_X = "merchantX";
    public static final String Merchant_Y = "merchantY";
    private String name, time, phone, address, ext;
    private MerchantDetailBean.MerchantServiceBean merchantServiceBean;
    private CustomWebView mWebView;
    private String loadUrl;
    private String merchant_id, x, y;
    private IOSAlertDialog callDialog;
    private CommonTitleRightWindow moreWindow;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_merchant_introduce);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        TitleBarHelper titleBarHelper = new TitleBarHelper(this, R.string.common_empty, R.string
                .common_empty, R.string.Merchant_producetitle);
        titleBarHelper.setRightImg(R.mipmap.icon_common_menu);
        titleBarHelper.setOnLeftClickListener(this);
        titleBarHelper.setOnRightImgClickListener(this);
        tv_produce_name = (TextView) findViewById(R.id.tv_produce_name);
        tv_produce_time = (TextView) findViewById(R.id.tv_produce_time);
        tv_produce_tel = (TextView) findViewById(R.id.tv_produce_tel);
        tv_produce_location = (TextView) findViewById(R.id.tv_produce_location);
        tv_wifi = (TextView) findViewById(R.id.free_wifi_tag);
        tv_drinks = (TextView) findViewById(R.id.free_drinks_tag);
        tv_parking = (TextView) findViewById(R.id.free_parking_tag);
        tv_gift = (TextView) findViewById(R.id.gift_tag);
        tv_dessert = (TextView) findViewById(R.id.dessert_tag);
        tv_door = (TextView) findViewById(R.id.door_service_tag);
        tv_produce_tel.setOnClickListener(this);
        tv_produce_location.setOnClickListener(this);
        moreWindow = new CommonTitleRightWindow(aty);
        initWebView();
        ShowWebView(Integer.parseInt(getString(R.string.common_lang)));
        mWebView.loadUrl(loadUrl);
        tv_produce_name.setText(name);
        tv_produce_time.setText(getString(R.string.Merchant_time) + time);
        if (!StringUtils.isEmpty(ext)) {
            tv_produce_tel.setText(StringUtils.formatTaiPhone(phone) + getString(R.string.common_ext) + ext);
        } else {
            tv_produce_tel.setText(StringUtils.formatTaiPhone(phone));
        }
        tv_produce_location.setText(address);
        showServicesKeywords(merchantServiceBean);
    }

    private void initWebView() {
        mWebView = (CustomWebView) findViewById(R.id.produce_webview);
        hideBuiltInZoomControls(mWebView);
        mWebView.setCanTouchZoom(false);

        mWebView.setDownloadListener(new DownloadListener() {// 下载
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,
                                        long contentLength) {
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                mWebView.setVerticalScrollBarEnabled(false);
                mWebView.setVerticalScrollbarOverlay(false);
                mWebView.setHorizontalScrollBarEnabled(false);
                mWebView.setHorizontalScrollbarOverlay(false);
                mWebView.setBackgroundColor(Color.parseColor("#ffffff"));
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                WebSettings settings = mWebView.getSettings();
                settings.setUseWideViewPort(true);
                settings.setSupportZoom(true);
                settings.setJavaScriptEnabled(true);

            }
        });
    }

    /**
     * 判断语言加载网页
     *
     * @param lang 1：中文 2：英文 3：泰文
     */
    private void ShowWebView(int lang) {
        switch (lang) {
            case 1:
                loadUrl = UrlConstants.HOST + "html5/partner_show.html?lang=1&MerchantID=" + merchant_id;
                break;
            case 2:
                loadUrl = UrlConstants.HOST + "html5/partner_show.html?lang=2&MerchantID=" + merchant_id;
                break;
            case 3:
                loadUrl = UrlConstants.HOST + "html5/partner_show.html?lang=3&MerchantID=" + merchant_id;
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        if (intent != null) {
            name = intent.getStringExtra(Introduce_NAME);
            time = intent.getStringExtra(Introduce_TIME);
            phone = intent.getStringExtra(Merchant_PHONE);
            ext = intent.getStringExtra(Merchant_EXT);
            address = intent.getStringExtra(Merchant_ADDRESS);
            merchantServiceBean = intent.getParcelableExtra(Merchant_SERVICE);
            merchant_id = intent.getStringExtra(MerchantDetailActivity.Merchant_ID);
            x = intent.getStringExtra(Merchant_X);
            y = intent.getStringExtra(Merchant_Y);
            merchant_id = RsaHelper.encryptDataFromStr(merchant_id, JSONWrapAjaxParams.publicKey);
        }
    }

    private void showServicesKeywords(MerchantDetailBean.MerchantServiceBean merchantServices) {

        if (merchantServices == null) {
            return;
        }
        if (merchantServices.isIsFreeWifi()) {
            tv_wifi.setVisibility(View.VISIBLE);
        }
        if (merchantServices.isIsFreeDrinks()) {
            tv_drinks.setVisibility(View.VISIBLE);
        }
        if (merchantServices.isIsFreeParking()) {
            tv_parking.setVisibility(View.VISIBLE);
        }
        if (merchantServices.isIsGift()) {
            tv_gift.setVisibility(View.VISIBLE);
        }
        if (merchantServices.isIsDessert()) {
            tv_dessert.setVisibility(View.VISIBLE);
        }
        if (merchantServices.isIsDoorService()) {
            tv_door.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.left:
                finish();
                break;
            case R.id.title_right_iv://悬浮菜单
                moreWindow.showWindow(findViewById(R.id.title_right_iv), -UIUtils.dip2px(110), 0);
                break;
            case R.id.tv_produce_tel://弹出电话拨打框
                if (callDialog == null) {
                    callDialog = DialogUtils.getInstance().createTitleDialog(this, StringUtils.formatTaiPhone(phone));
                    callDialog.setCancelText(getString(R.string.common_calcle));
                    callDialog.setConfirmText(getString(R.string.comm_call_phone));
                    callDialog.setConfirmClickListener(new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            callDialog.dismiss();
                            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    });
                }
                callDialog.show();
                break;
            case R.id.tv_produce_location://跳至地图导航页
                if (address == null) {
                    return;
                }
                //打开地图
                Intent intent = new Intent(this, GMapActivity.class);
                intent.putExtra(GMapActivity.MERCHANTNAME, name);
                intent.putExtra(GMapActivity.MERCHANTADDRESS, address);
                intent.putExtra(GMapActivity.MERCHANTLAT, x);
                intent.putExtra(GMapActivity.MERCHANTLNG, y);
                this.startActivity(intent);
                break;
        }
    }

    private void hideBuiltInZoomControls(WebView view) {
        if (Build.VERSION.SDK_INT < 11) {
            try {
                Field field = WebView.class.getDeclaredField("mZoomButtonsController");
                field.setAccessible(true);
                ZoomButtonsController zoomCtrl = new ZoomButtonsController(view);
                zoomCtrl.getZoomControls().setVisibility(View.GONE);
                field.set(view, zoomCtrl);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                WebSettings settings = view.getSettings();
                Method method = WebSettings.class.getMethod("setDisplayZoomControls", boolean.class);
                method.invoke(settings, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
